home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / listdrag / listdrag.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-09-06  |  4.7 KB  |  149 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "ListDrag Demo"
  4.    ClientHeight    =   3195
  5.    ClientLeft      =   1875
  6.    ClientTop       =   3660
  7.    ClientWidth     =   5865
  8.    Height          =   3600
  9.    Left            =   1815
  10.    LinkMode        =   1  'Source
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   3195
  13.    ScaleWidth      =   5865
  14.    Top             =   3315
  15.    Width           =   5985
  16.    Begin ListBox List1 
  17.       FontBold        =   -1  'True
  18.       FontItalic      =   0   'False
  19.       FontName        =   "Courier"
  20.       FontSize        =   12
  21.       FontStrikethru  =   0   'False
  22.       FontUnderline   =   0   'False
  23.       Height          =   270
  24.       Left            =   480
  25.       TabIndex        =   0
  26.       Top             =   480
  27.       Width           =   4935
  28.    End
  29.    Begin Label Label2 
  30.       Caption         =   "Hold down Ctrl key and use Up and Down arrow keys to move a line in the listbox.  Or press Ctrl and drag a line with the mouse."
  31.       Height          =   620
  32.       Left            =   360
  33.       TabIndex        =   2
  34.       Top             =   2400
  35.       Width           =   5175
  36.    End
  37.    Begin Label Label1 
  38.       Enabled         =   0   'False
  39.       Height          =   10
  40.       Left            =   0
  41.       TabIndex        =   1
  42.       Top             =   0
  43.       Visible         =   0   'False
  44.       Width           =   10
  45.    End
  46. ' LISTSWAP.MAK a demonstration Visual Basic program to show
  47. ' how single items in a list box can be reordered using
  48. ' Ctrl-UpArrow/DownArrow or by pressing Ctrl and dragging
  49. ' a list item with the mouse.
  50. ' Sue Mosher, 202-736-1136, CIS 75140,543
  51. ' Public domain
  52. Dim MoveLine As Integer     ' values: -1 for UP move,
  53.                             ' 1 for DOWN, 0 for none
  54. Dim Item1 As Integer        ' line to be moved
  55. Dim RowSize As Integer
  56. Dim MoveNow As Integer
  57. Const ROWS = 5
  58. Const TRUE = -1
  59. Const FALSE = 0
  60. Const CTRL = 2
  61. Const KEY_UP = &H26
  62. Const KEY_DOWN = &H28
  63. Sub Form_Load ()
  64.     List1.Height = 20 * ROWS * List1.FontSize
  65.     RowSize = List1.Height / ROWS
  66.     For I = 1 To ROWS
  67.         List1.AddItem ("Item " + Str$(I))
  68.     Next I
  69.     List1.ListIndex = 0
  70.     MoveNow = False
  71. End Sub
  72. Sub List1_DragDrop (Source As Control, X As Single, Y As Single)
  73.     MoveNow = False
  74.     Label1.Enabled = False
  75.     List1.SetFocus
  76. End Sub
  77. Sub List1_DragOver (Source As Control, X As Single, Y As Single, State As Integer)
  78.     Select Case State
  79.         Case 1             ' if leaving list, turn off
  80.             Label1.Drag 2   ' drag & force drop
  81.         Case 2
  82.             If MoveRow(Y) <> 0 Then  ' if within move range
  83.                 Item2% = Item1% + MoveRow(Y)
  84.                 ListSwap Item1%, Item2%, List1
  85.                 Item1% = Item2%
  86.                 List1.ListIndex = Item1%
  87.             End If
  88.     End Select
  89. End Sub
  90. Sub List1_KeyDown (KeyCode As Integer, Shift As Integer)
  91.     CtrlDown% = (Shift And CTRL) > 0
  92.     UpPressed% = (KeyCode = KEY_UP)
  93.     DownPressed% = (KeyCode = KEY_DOWN)
  94.     If CtrlDown% And UpPressed% Then
  95.         Item1% = List1.ListIndex        ' set item to be moved
  96.         If Item1% > 0 Then
  97.             MoveLine = -1
  98.         Else
  99.             MoveLine = 0
  100.              Beep
  101.         End If
  102.     End If
  103.     If CtrlDown% And DownPressed% Then
  104.         Item1% = List1.ListIndex
  105.         If Item1% < (List1.ListCount - 1) Then
  106.             MoveLine = 1
  107.         Else
  108.             MoveLine = 0
  109.             Beep
  110.         End If
  111.     End If
  112.     If MoveLine <> 0 Then ListSwap Item1%, (Item1% + MoveLine), List1
  113. End Sub
  114. Sub List1_KeyUp (KeyCode As Integer, Shift As Integer)
  115.     CtrlDown% = (Shift And CTRL) > 0
  116.     If Not CtrlDown% Then MoveLine = 0
  117. End Sub
  118. Sub List1_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
  119.     CtrlDown% = (Shift And CTRL) > 0
  120.     If CtrlDown% Then
  121.         Item1% = List1.ListIndex
  122.         Label1.Tag = Str$(Item1%)   ' get item to be moved
  123.         MoveNow = True
  124. '       move label control to mouse position and start
  125. '       dragging it
  126.         Label1.Enabled = True
  127.         Label1.Move (List1.Left + X), (List1.Top + Y)
  128.         Label1.Drag 1
  129.     End If
  130. End Sub
  131. Sub ListSwap (Line1 As Integer, Line2 As Integer, ListBox As Control)
  132.     Temp$ = ListBox.List(Line1)
  133.     ListBox.List(Line1) = ListBox.List(Line2)
  134.     ListBox.List(Line2) = Temp$
  135. End Sub
  136. Function MoveRow (Y As Single)
  137.     Offset% = (Y \ RowSize) - Item1%
  138.     If Abs(Offset%) = 1 Then        ' if within 1 row
  139.         MoveRow = Offset%
  140.     Else
  141.         MoveRow = 0
  142.     End If
  143. End Function
  144. Sub SwapInt (Int1%, Int2%)
  145.     TempInt% = Int1%
  146.     Int1% = Int2%
  147.     Int2% = TempInt%
  148. End Sub
  149.